import random
import time
import heapq

# 生成随机数组
def make_random_list(n, low=1, high=100):
    nums = []
    for _ in range(n):
        nums.append(random.randint(low, high))
    return nums

def find_kth(nums, k):
    heap = []
    for x in nums:
        if len(heap) < k:
            heapq.heappush(heap, x)
        else:
            if x > heap[0]:
                heapq.heapreplace(heap, x)
    return heap[0]

n =100000
k =200
nums = make_random_list(n, low=1, high=100000)

t1 = time.perf_counter()
ans = find_kth(nums, k)
t2 = time.perf_counter()

print("数组长度:", n)
print("k =", k)
print("第k个最大的元素:", ans)
print("运行时间: %.4f ms" % ((t2 - t1) * 1000))
